using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Model.Page;
using Keep.Service;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class BlogController : ControllerBase
{
    //博客请求参数类
    public class BlogRequest
    {
        public string? path { get; set; }
        public string? oldname { get; set; }
        public string? newname { get; set; }
        public string? name { get; set; }
        public string? keypath { get; set; }
        public string? content { get; set; }
        public string? selname { get; set; }
        public int currentPage { get; set; }
        public int pageSize { get; set; }
    }

    private readonly IFileService fileService;

    public BlogController(IFileService fileService)
    {
        this.fileService = fileService;
    }

    [HttpPost(Name = "AddBlog")]
    public async Task<ActionResult<ResultData>> AddBlog(BlogRequest blogRequest)
    {
        Log.Warning("IN => " + blogRequest.path);
        bool sys = await Task.Run(() => fileService.AddFile(blogRequest.path, Static.BlogPath));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "AddBlogDirectory")]
    public async Task<ActionResult<ResultData>> AddBlogDirectory(BlogRequest blogRequest)
    {
        Log.Warning("IN => " + blogRequest.path);
        string sys = await Task.Run(() => fileService.AddFileDirectory(blogRequest.path, Static.BlogPath));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "DelBlog")]
    public async Task<ActionResult<ResultData>> DelBlog(BlogRequest blogRequest)
    {
        Log.Warning("IN =>   path=" + blogRequest.path + "  name=" + blogRequest.name);
        bool sys = await Task.Run(() => fileService.DelFile(blogRequest.path, Static.BlogPath, blogRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "DelBlogDir")]
    public async Task<ActionResult<ResultData>> DelBlogDir(BlogRequest blogRequest)
    {
        Log.Warning("IN =>   path=" + blogRequest.path + "  name=" + blogRequest.name);
        bool sys = await Task.Run(() => fileService.DelFileDir(blogRequest.path, Static.BlogPath, blogRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }

    [HttpPost(Name = "EditBlogDirOrName")]
    public async Task<ActionResult<ResultData>> EditBlogDirName(BlogRequest blogRequest)
    {
        Log.Warning("IN => path=" + blogRequest.path + "    oldname=" + blogRequest.oldname + "    newname=" + blogRequest.newname);
        bool sys = await Task.Run(() => fileService.EditFileDirOrName(blogRequest.path, Static.BlogPath, blogRequest.oldname, blogRequest.newname));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }

    [HttpPost(Name = "GetBlogPathInfo")]
    public async Task<ActionResult<ResultData>> GetBlogPathInfo(BlogRequest blogRequest)
    {
        Log.Warning("IN => " + blogRequest.path);
        PageResult<FileDiy> sys = await Task.Run(() => fileService.GetPathInfo(Static.BlogPath, blogRequest.path, blogRequest.currentPage, blogRequest.pageSize));
        Log.Warning("OUT => " + sys.ToString() + "  数量:" + sys.Rows.Count);
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "GetBlogData")]
    public async Task<ActionResult<ResultData>> GetBlogData(BlogRequest blogRequest)
    {
        Log.Warning("IN => " + blogRequest.keypath);
        string data = await Task.Run(() => fileService.GetFileData(Static.BlogPath, blogRequest.keypath));
        Log.Warning("OUT => " + data);
        return Ok(new ResultData(Static.Enable, data));
    }
    [HttpPost(Name = "SelBlogByName")]
    public async Task<ActionResult<ResultData>> SelBlogByName(BlogRequest blogRequest)
    {
        Log.Warning("IN => " + blogRequest.keypath);
        List<FileDiy> data = await Task.Run(() => fileService.SelFileByName(Static.BlogPath, blogRequest.selname));
        Log.Warning("OUT => " + data);
        return Ok(new ResultData(Static.Enable, data));
    }
    [HttpPost(Name = "SaveBlogData")]
    public async Task<ActionResult<ResultData>> SaveBlogData(BlogRequest blogRequest)
    {
        Log.Warning("IN => " + blogRequest.keypath);
        bool sys = await Task.Run(() => fileService.SaveFileData(Static.BlogPath, blogRequest.keypath, blogRequest.content));
        Log.Warning("OUT => " + sys);
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "UploadBlogImg")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadBlogImg(IFormFile file)
    {
        Log.Warning("UploadBlogImg => IN");
        string sys = fileService.UploadBlogImage(file);
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "UploadBlog")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadBlog(List<IFormFile> file, string? fileurl)
    {
        try
        {
            if (file.Count > 0)
            {
                if (String.IsNullOrEmpty(fileurl))
                {
                    fileurl = Request.Form["fileurl"];
                }
                Log.Warning("UploadBlog => " + fileurl);
                fileService.UploadFile(file, fileurl, Static.BlogPath);
                return Ok(new ResultData(Static.Enable, file[0].FileName));
            }
            else
            {
                return BadRequest("没有发现上传文件");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }
}
